package com.shlh.saas.service.impl;

import com.shlh.saas.entity.RechargeRecord;
import com.shlh.saas.mapper.RechargeRecordMapper;
import com.shlh.saas.service.RechargeRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 充值记录服务实现类
 */
@Service
public class RechargeRecordServiceImpl implements RechargeRecordService {
    
    private static final Logger log = LoggerFactory.getLogger(RechargeRecordServiceImpl.class);
    
    @Autowired
    private RechargeRecordMapper rechargeRecordMapper;

    @Override
    public boolean createRecord(String phone, String type, BigDecimal amount, BigDecimal afterAmount,
                              String description, String payMethod, Integer packageId) {
        log.info("创建交易记录: 用户={}, 类型={}, 金额={}, 交易后余额={}, 描述={}, 支付方式={}, 套餐ID={}",
                phone, type, amount, afterAmount, description, payMethod, packageId);
        
        try {
            RechargeRecord record = new RechargeRecord();
            record.setPhone(phone);
            record.setRechargeType(type);
            record.setAmount(amount);
            
            // 设置配额增量，根据套餐类型和ID设置
            Integer quotaAdded = 0;
            if ("VIP".equals(type) && packageId != null) {
                // 根据VIP套餐ID设置配额
                switch (packageId) {
                    case 2: // 初级VIP
                        quotaAdded = 100;
                        break;
                    case 3: // 高级VIP
                        quotaAdded = 300;
                        break;
                    case 4: // 尊享VIP
                        quotaAdded = 1000;
                        break;
                    default:
                        quotaAdded = 0;
                }
            } else if ("EMAIL".equals(type)) {
                // 针对邮件套餐设置配额
                if (packageId != null) {
                    switch (packageId) {
                        case 1: // 基础邮件套餐
                            quotaAdded = 100;
                            break;
                        case 2: // 进阶邮件套餐
                            quotaAdded = 300;
                            break;
                        case 3: // 高级邮件套餐
                            quotaAdded = 1000;
                            break;
                        default:
                            quotaAdded = 0;
                    }
                }
            }
            
            record.setQuotaAdded(quotaAdded);
            record.setDescription(description);
            // 设置套餐ID
            record.setPackageId(packageId);
            // 生成订单号
            record.setOrderNo(generateOrderNo());
            record.setPaymentMethod(payMethod);
            record.setStatus(0); // 0表示成功
            record.setCreatedAt(LocalDateTime.now());
            record.setUpdatedAt(LocalDateTime.now());
            
            int rows = rechargeRecordMapper.insert(record);
            log.info("交易记录创建结果: {}", rows > 0 ? "成功" : "失败");
            return rows > 0;
        } catch (Exception e) {
            log.error("创建交易记录时发生异常: {}", e.getMessage(), e);
            return false;
        }
    }

    @Override
    public List<RechargeRecord> getUserRecords(String phone, String type) {
        log.info("查询用户{}的交易记录, 类型={}", phone, type);
        try {
            List<RechargeRecord> records = rechargeRecordMapper.selectUserRecords(phone, type);
            log.info("查询到{}条交易记录", records != null ? records.size() : 0);
            return records;
        } catch (Exception e) {
            log.error("查询用户{}的交易记录时发生异常: {}", phone, e.getMessage(), e);
            return null;
        }
    }
    
    /**
     * 生成订单号
     * @return 订单号
     */
    private String generateOrderNo() {
        return "ORDER" + System.currentTimeMillis() + UUID.randomUUID().toString().substring(0, 8);
    }
} 